/* Appendix Table E9 */

data output.uganda;
	set UgandaCensus;
	if DISTRICT=301 then demand2=309;
	else if DISTRICT=201 then demand2=203;
	else if DISTRICT=202 then demand2=212;
	else if DISTRICT=207 then demand2=211;
	else if DISTRICT=109 then demand2=104;
	else if DISTRICT=111 then demand2=105;
	else if DISTRICT=413 then demand2=405;
	else if DISTRICT=112 then demand2=108;
	else if DISTRICT=415 then demand2=405;
	else if DISTRICT=214 then demand2=203;
	else if DISTRICT=312 then demand2=305;
	else if DISTRICT=215 then demand2=209;
	else if DISTRICT=113 then demand2=106;
	else if DISTRICT=313 then demand2=303;
	else if DISTRICT=213 then demand2=211;
	else if DISTRICT=414 then demand2=412;
	else if DISTRICT=311 then demand2=308;
	else demand2=DISTRICT;
run;



/*Analysis for POST-Splitting */
proc sql;
	create table num as select DISTRICT, P7_ETHNICITY, count(district) as numerator
	from output.uganda
	group by p7_ETHNICITY, DISTRICT
	order by p7_ETHNICITY, DISTRICT;
quit;

proc sql;
	create table numdenom as select unique DISTRICT, numerator,
	sum(numerator) as denominator
	from num
	group by DISTRICT;
quit;

data mkt_shr;
	set numdenom;
	market_share_sqr = (numerator/denominator) * (numerator/denominator);
	if denominator= 0 then delete;
run;

proc sql;
	create table output.ethnicityHHIPOST as
  	select DISTRICT, sum(market_share_sqr) as Herfindahl
  	from mkt_shr
  	group by DISTRICT;
quit;

/*Analysis for PRE-Splitting*/
proc sql;
	create table num as select demand2, P7_ETHNICITY, count(demand2) as numerator
	from output.uganda
	group by p7_ETHNICITY, demand2
	order by p7_ETHNICITY, demand2;
quit;

proc sql;
	create table numdenom as select unique demand2, numerator,
	sum(numerator) as denominator
	from num
	group by demand2;
quit;

data mkt_shr;
	set numdenom;
	market_share_sqr = (numerator/denominator) * (numerator/denominator);
	if denominator= 0 then delete;
run;

proc sql;
	create table output.ethnicityHHIPRE as
  	select demand2, sum(market_share_sqr) as Herfindahl
  	from mkt_shr
  	group by demand2;
quit;
